﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using AjaxControlToolkit;
 /*
  验证辅助控件基类
  席吉波
  * 2011-08-13
  */
namespace Kevin.Controls
{
    /// <summary>
    /// 神州数码 -  自定义验证控件基类INamingContainer
    /// </summary>    
    [TargetControlType(typeof(TextBox))]
    [NonVisualControl]
    public  abstract  class ValidationBase:ExtenderControlBase
    {
        #region [  Properties  ]

        ///// <summary>
        ///// 需验证控件ID
        ///// </summary>
        //public string TargetControlID
        //{
        //    get;
        //    set;
        //}
        /// <summary>
        /// 必录
        /// </summary>
        public bool Required
        {
            get;
            set;
        }
        public string RequiredErrorMessage
        {
            get;
            set;
        }
        /// <summary>
        /// 验证组
        /// </summary>
        public string ValidationGroup
        {
            get;
            set;
        }
        /// <summary>
        /// 自定义正则表达式
        /// </summary>
        public virtual string CustomExpression
        {
            get;
            set;
        }

        /// <summary>
        /// 自定义验证错误信息
        /// </summary>
        public string CustomExpressionErrorMessage
        {
            get;
            set;
        }

        #endregion

        #region 

        /// <summary>
        /// 设置 RequiredValidator
        /// </summary>
        protected virtual void SetRequiredValidator()
        { 
             if (this.Required)
            {
                RequiredFieldValidator req_val = new RequiredFieldValidator();
                req_val.ID = this.TargetControlID + "_RequiredFieldValidator";
                req_val.ControlToValidate = this.TargetControlID;
                req_val.ValidationGroup = this.ValidationGroup;
                req_val.ErrorMessage = this.RequiredErrorMessage;
               
                this.Controls.Add(req_val);
            }
        }

        /// <summary>
        /// 设置自定义正则表达式验证验证控件
        /// </summary>
        public virtual void SetRegularExpresstionValidator()
        {
            if (!string.IsNullOrEmpty(this.CustomExpression))
            {
                RegularExpressionValidator reg_val = new RegularExpressionValidator();
                reg_val.ID = this.TargetControlID + "_RegularExpressionValidator";
                reg_val.ControlToValidate = this.TargetControlID;
                reg_val.ValidationExpression = this.CustomExpression;
                reg_val.ValidationGroup = this.ValidationGroup;
                reg_val.ErrorMessage = this.CustomExpressionErrorMessage;
                this.Controls.Add(reg_val);
            }
        }

        #endregion

        #region [ Override methodes ]

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            if (!DesignMode)
            {
                SetRequiredValidator();
                SetRegularExpresstionValidator();
            }
            base.Render(writer);
        }

        protected override IEnumerable<ScriptDescriptor> GetScriptDescriptors(Control targetControl)
        {
            return null;
        }

        protected override IEnumerable<ScriptReference> GetScriptReferences()
        {
            return null;
            //throw new NotImplementedException();
        }

        #endregion
    }
}
